﻿// StartScanCommand
// Command for stopping scan
// 
// FLMScan - Featured by FabLab Munich

using System;
using System.Windows.Input;

namespace FLMScan.Commands
{
    internal class StopScanCommand : ICommand
    {
        private FLMScanViewModel viewModel;
        private StateMachine stateMachine;

        public event EventHandler CanExecuteChanged;

        public StopScanCommand(FLMScanViewModel viewModel, StateMachine stateMachine)
        {
            this.viewModel = viewModel;
            this.stateMachine = stateMachine;

            // stop command is inactive in idle state only
            stateMachine.OnIdleState += HandleCommandActivationChange;
            // stop command is active in laser scanning, light scanning and calibrating state
            stateMachine.OnLaserscanningState += HandleCommandActivationChange;
            stateMachine.OnLightScanningState += HandleCommandActivationChange;
            stateMachine.OnCalibratingState += HandleCommandActivationChange;                        
        }

        public void Execute(object parameter)
        {
            String text = (String)parameter;

            viewModel.OnStopScanCommand();
        }

        public bool CanExecute(object parameter)
        {
            return stateMachine.TryHandleAction(StateMachine.FlmScanAction.EActionStopButton);            
        }

        private void HandleCommandActivationChange(object sender, EventArgs e)
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, EventArgs.Empty);                
            }
        }            
    };
}
